package com.itchen.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * @author げっしょく
 * @date 2021/8/28
 */
@WebServlet("/demo1")
public class Test1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //String getMethod():获取请求方式: GET
        String method = req.getMethod();
        System.out.println(method);
        //String getContextPath():获取虚拟目录(项目访问路径): /request-demo
        String contextPath = req.getContextPath();
        System.out.println(contextPath);
        //StringBuffer getRequestURL():获取URL(统一资源定位符): http://ocalhost:8080/request-demo/req1
        StringBuffer requestURL = req.getRequestURL();
        System.out.println(requestURL.toString());
        //String getRequestURI():获取URI(统一资源标识符): /request-demo/req1
        String requestURI = req.getRequestURI();
        System.out.println(requestURI);
        //String getQueryString():获取请求参数(GET方式) : username= zhangsan&password=123
        String queryString = req.getQueryString();
        System.out.println(queryString);

        String header = req.getHeader("user-agent");
        System.out.println(header);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader reader = req.getReader();
        String s = reader.readLine();
        System.out.println(s);
    }
}
